import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lenovo
 * Date: 2022-08-26
 * Time: 17:39
 */
public class Test {

    public  static  int[] smallestK(int[] arr, int k) {
        int[] small = new int[k];
        if(k == 0){
            return small;
        }
        PriorityQueue<Integer> maxPQ = new PriorityQueue<>(k,new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;//构建大堆
            }
        });
        //遍历数组,调整大堆
        for(int i = 0; i< arr.length;i++){
            //构建k个元素的大堆
            if(maxPQ.size() < k){
                maxPQ.offer(arr[i]);
            }else {//[k,len-1]范围里的元素与堆顶元素进行比较
                //获取堆顶的元素
                int top = maxPQ.peek();
                if(top > arr[i]){
                    maxPQ.poll();
                    maxPQ.offer(arr[i]);
                }
            }

        }
        for (int i = 0; i < k; i++) {
            small[i] = maxPQ.poll();

        }
        return small;


    }
    public static void main(String[] args) {
        //最小K个数
        int[] arr = {15,56,78,93,5,41,11};
        smallestK(arr,3);
    }
    static  class AgeComparator implements Comparator<Student>{
        @Override
        public int compare(Student o1, Student o2) {
            return o1.age-o2.age;
        }
    }
     static class Student /*implements Comparable<Student>*/{
        int age;

         public Student(int age) {
             this.age = age;
         }

//         @Override
//         public int compareTo(Student o) {
//             return this.age - o.age;
//         }
     }


    public static void small_big(int[] arr, int k,int len) {

         //构建大根堆
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;//构建大根堆
            }
        });
        for (int i = 0; i < arr.length; i++) {
            priorityQueue.offer(arr[i]);
        }


    }


    public static int[] bigeststK(int[] arr, int k,int len) {
        int[] maxK = new int[k];

        //构建大根堆
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;//构建大根堆
            }
        });
        for (int i = 0; i < arr.length; i++) {
            priorityQueue.offer(arr[i]);
        }
        for (int i = 0; i < k; i++) {
            maxK[i] = priorityQueue.poll();
        }
        return maxK;


    }
    public static void main4(String[] args) {
        //求前k个大值
        int[] arr = {12,56,54,96,12};
        int[] maxK = bigeststK(arr,3,arr.length);
    }
    public static void main3(String[] args) {
         Student student1 = new Student(15);
         Student student2 = new Student(50);
        AgeComparator  ageComparator = new AgeComparator();
        //PriorityQueue<Student> priorityQueue = new PriorityQueue<>(ageComparator);
        //匿名内部类
        PriorityQueue<Student> priorityQueue = new PriorityQueue<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return 0;
            }
        });
        priorityQueue.offer(student1);
        priorityQueue.offer(student2);
        System.out.println("deffdfdf");
    }
    public static void main2(String[] args) {
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
        priorityQueue.offer(1);
        priorityQueue.offer(100);
        priorityQueue.offer(1000);
        priorityQueue.offer(10000);
        priorityQueue.offer(100000);
        System.out.println(priorityQueue.peek());//默认为小根堆
    }
    public static void main1(String[] args) {
        TreeHeap heap = new TreeHeap();
        int[] array = { 27,15,19,18,28,34,65,49,25,37};
        heap.inital(array);
        heap.create();
//        heap.offer(80);
        heap.poll();
        System.out.println("sjhdjksgdksga");

    }
    public static void main7(String[] args) {
        TreeHeap heap = new TreeHeap();
        int[] array = { 27,15,19,18,28,34,65,49,25,37};
        heap.inital(array);
        heap.create();
        heap.heapSort();
        System.out.println("sjhdjksgdksga");

    }
}